﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;

namespace DomainModel.Concrete
{
    // Implementation of the Educations section of the database.
    // The middle layer of the DAL.
    class SqlEducationsRepository : IEducationsRepository
    {
        private Table<Education> educationsTable;

        public SqlEducationsRepository(string connectionString)
        {
            educationsTable = (new DataContext(connectionString)).GetTable<Education>();
        }

        public IQueryable<Education> Educations
        {
            get { return educationsTable; }
        }

        public void SaveEducation(Education education)
        {
            if (education.EducationID == 0)
            {
                educationsTable.InsertOnSubmit(education);
            }
            else
            {
                educationsTable.Attach(education);
                educationsTable.Context.Refresh(RefreshMode.KeepCurrentValues, education);
            }

            educationsTable.Context.SubmitChanges();
        }

        public void DeleteEducation(Education education)
        {
            educationsTable.DeleteOnSubmit(education);
            educationsTable.Context.SubmitChanges();
        }
    }
}